package com.gs.SynPatrolRep.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.activity.event.CreateReportActivity;
import com.gs.SynPatrolRep.activity.palBill.PalBillItemActivity;
import com.gs.SynPatrolRep.adapter.PalBillEventAdapter;
import com.gs.SynPatrolRep.adapter.base.BaseAdapter;
import com.gs.SynPatrolRep.apiresult.ApiResultData;
import com.gs.SynPatrolRep.apiresult.PagedList;
import com.gs.SynPatrolRep.constant.AllConstants;
import com.gs.SynPatrolRep.db.dto.CreatePalBillEventResultDto;
import com.gs.SynPatrolRep.db.dto.PalBillEventDto;
import com.gs.SynPatrolRep.db.dto.ShortDto;
import com.gs.SynPatrolRep.db.query.PatrolBillListQuery;
import com.gs.SynPatrolRep.dialog.ReportResultDialog;
import com.gs.SynPatrolRep.dialog.ToastDialogUtils;
import com.gs.SynPatrolRep.fragment.base.BaseOptionListFragment;
import com.gs.SynPatrolRep.presenter.PalBillEventPresenter;
import com.gs.SynPatrolRep.view.IPalBillEventView;

import java.util.ArrayList;
import java.util.List;

public class SiteEventListFragment extends BaseOptionListFragment<IPalBillEventView, PalBillEventPresenter, PalBillEventDto, PatrolBillListQuery>
        implements IPalBillEventView {

    private PalBillEventAdapter adapter;
    private String id;

    @Override
    public void initView() {
        query = new PatrolBillListQuery();
        recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        adapter = new PalBillEventAdapter(context, list, R.layout.rvitem_event);
        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View v, int position) {
                PalBillEventDto dto = list.get(position);
                if (dto.getPro() == 0) {
                    id = dto.getId();
                    mPresenter.getData(id);
                }else {
                    Intent intent = new Intent(context, CreateReportActivity.class);
                    intent.putExtra(AllConstants.ID,dto.getId());
                    intent.putExtra(AllConstants.INFO_TYPE,3);
                    startActivity(intent);
                }
            }
        });
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(null);
        fd(R.id.btn_create).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CreateReportActivity.class);
                intent.putExtra(AllConstants.INFO_TYPE,0);
                startActivityForResult(intent, 10);
            }
        });
    }

    @Override
    public void initData() {
        query.init();
        query.setKeyWord(toolBarSearch.getText());
        mPresenter.getData(query);
    }

    @Override
    public void loadData() {
        mPresenter.getData(query);
    }

    @Override
    public void loadOptionData() {
        List<ShortDto> listShort = new ArrayList<>();
        listShort.add(new ShortDto("0", getString(R.string.un_process)));
        toolBarOption.setList(listShort);
    }

    @Override
    public void add() {

    }

    @Override
    public void search() {
        list.clear();
        adapter.notifyDataSetChanged();
        query.init();
        query.setKeyWord(toolBarSearch.getText());
        mPresenter.getData(query);
    }

    @Override
    public void optionItem(Object option) {
        if (option == null) return;
        ShortDto dto = (ShortDto) option;
        String id = dto.getId();
        query.setPro(TextUtils.isEmpty(id)?null:Integer.parseInt(id));
        search();
    }

    @Override
    public void delete(int position) {

    }

    @Override
    public void update(int position) {

    }

    @Override
    public PalBillEventPresenter createPresenter() {
        return new PalBillEventPresenter();
    }

    @Override
    public PatrolBillListQuery createQuery() {
        return new PatrolBillListQuery();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_pal_bill_event;
    }

    @Override
    public void onGetDataSuccess(ApiResultData<PagedList<PalBillEventDto>> apiResultData) {
        refreshSmartRefreshLayout();
        if (apiResultData.getStatus() != 2000) {
            ToastDialogUtils.getInstance().showInfoMessage(context, apiResultData.getMessage());
            return;
        }
        list.addAll(apiResultData.getData().getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onGetDataFail(Exception e) {
        ToastDialogUtils.getInstance().showInfoMessage(context, getString(R.string.network_error));
    }

    @Override
    public void onGetDataIdSuccess(ApiResultData<PalBillEventDto> apiResultData) {
        if (apiResultData.getStatus() != 2000) {
            ToastDialogUtils.getInstance().showInfoMessage(context, apiResultData.getMessage());
            return;
        }
        PalBillEventDto dto = apiResultData.getData();
        new ReportResultDialog(context, dto.getEventNum(), dto.getPhotos(), new ReportResultDialog.IChoiceListener() {
            @Override
            public void getChoiceFromDialog(CreatePalBillEventResultDto bean) {
                bean.setId(id);
                mPresenter.setResult(bean);
            }
        }).show();
    }

    @Override
    public void onGetDataIdFail(Exception e) {
        ToastDialogUtils.getInstance().showInfoMessage(context, getString(R.string.network_error));
    }

    @Override
    public void onSetResultSuccess(ApiResultData<String> apiResultData) {
        ToastDialogUtils.getInstance().showInfoMessage(context, apiResultData.getMessage());
        if (apiResultData.getStatus() == 2000) {
            search();
        }
    }

    @Override
    public void onSetResultFail(Exception e) {
        ToastDialogUtils.getInstance().showInfoMessage(context, getString(R.string.network_error));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10 && resultCode == 1) {
            list.clear();
            query.init();
            mPresenter.getData(query);
        }
    }
}